package site.codeblog.designpattern.singleton;

import java.io.*;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;

public class Client {

    public static void main(String[] args) throws NoSuchMethodException, InvocationTargetException, InstantiationException, IllegalAccessException, IOException, ClassNotFoundException {

        Class<Singleton> singletonClass = Singleton.class;
        Constructor<Singleton> declaredConstructor = singletonClass.getDeclaredConstructor();
        declaredConstructor.setAccessible(true);
        Singleton singleton1 = declaredConstructor.newInstance();
        Singleton singleton2 = declaredConstructor.newInstance();
        System.out.println(singleton1);
        System.out.println(singleton2);

        /*writeObjectToFile();
        readObjectFromFile();
        readObjectFromFile();*/

    }










    public static void readObjectFromFile() throws IOException, ClassNotFoundException {
        ObjectInputStream ois = new ObjectInputStream(new FileInputStream("/Applications/work/a.txt"));
        Singleton singleton = (Singleton) ois.readObject();
        System.out.println(singleton);
    }

    public static void writeObjectToFile() throws IOException {
        Singleton instance = Singleton.getInstance();
        System.out.println(instance);
        ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("/Applications/work/a.txt"));
        oos.writeObject(instance);
        oos.close();
    }

}
